package com.leng.Controller;

import com.github.pagehelper.PageInfo;
import com.leng.entity.AirQualityIndex;
import com.leng.entity.District;
import com.leng.service.AirQualityIndexService;
import com.leng.service.DistrictService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: 冷俊杰
 * @Description: TODO
 * @DateTime: 2022/8/1 17:00
 **/
@Controller
public class AirQualityIndexController {
    @Resource
    private AirQualityIndexService service;
    @Resource
    private DistrictService district;

    @GetMapping("/listAQ")
    public String listAQ(@RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum,
                         @RequestParam(name = "pageSize", required = false, defaultValue = "3") int pageSize,
                         Model model) {
        PageInfo<AirQualityIndex> airQualityIndexPageInfo = service.pageInfo(pageNum, pageSize);
        model.addAttribute("pageInfo", airQualityIndexPageInfo);
        return "index";
    }

    @RequestMapping("/deleteAQ")
    public ModelAndView deleteAQ(int id, @RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum,
                                 @RequestParam(name = "pageSize", required = false, defaultValue = "3") int pageSize) {
        ModelAndView modelAndView = new ModelAndView();
        service.deleteByPrimaryKey(id);
        modelAndView.addObject("pageInfo", service.pageInfo(pageNum, pageSize));
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @GetMapping("/addAQ")
    public String addAQ(@RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum,
                        @RequestParam(name = "pageSize", required = false, defaultValue = "3") int pageSize,
                        Model model, AirQualityIndex qualityIndex) {
        service.insert(qualityIndex);
        model.addAttribute("pageInfo", service.pageInfo(pageNum, pageSize));
        return "index";
    }

    @GetMapping("/selectByPrimaryKey")
    public String selectByPrimaryKey(@RequestParam("id") Integer id, @RequestParam("districid") Integer districid, Model model) {
        AirQualityIndex airQualityIndex = service.selectByPrimaryKey(id);
        LocalDateTime localDateTime =LocalDateTime.now();
        model.addAttribute("now",localDateTime);
        model.addAttribute("SK", airQualityIndex);
        model.addAttribute("districts", districid);
        return "update";
    }

    @GetMapping("/updateAQ")
    public String updateAQ(AirQualityIndex qualityIndex, Model model,
                           @RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum,
                           @RequestParam(name = "pageSize", required = false, defaultValue = "3") int pageSize) {
        System.out.println(service.updateByPrimaryKey(qualityIndex));
        model.addAttribute("pageInfo", service.pageInfo(pageNum, pageSize));
        return "index";
    }

    @GetMapping("/listAQs")
    public String listAQs(@RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum,
                         @RequestParam(name = "pageSize", required = false, defaultValue = "3") int pageSize,
                         Model model) {
        PageInfo<AirQualityIndex> airQualityIndexPageInfo = service.pageInfo(pageNum, pageSize);
        List<District> districts = district.listDistrict();
        model.addAttribute("pageInfo", airQualityIndexPageInfo);
        model.addAttribute("districts",districts);
        return "indexs";
    }

    @GetMapping("/pageInfoByAQ")
    public String pageInfoByAQ(@RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum,
                               @RequestParam(name = "pageSize", required = false, defaultValue = "3") int pageSize,
                               Model model,AirQualityIndex record){
        PageInfo<AirQualityIndex> airQualityIndexPageInfo = service.pageInfoByAQ(pageNum, pageSize,record);
        List<District> districts = district.listDistrict();
        model.addAttribute("pageInfo",airQualityIndexPageInfo);
        model.addAttribute("districts",districts);
        return "indexs";
    }
}